package cn.itcast.service.impl;


import cn.itcast.clients.CastClients;
import cn.itcast.clients.NoteClients;
import cn.itcast.clients.RoomClients;
import cn.itcast.clients.UserClients;
import cn.itcast.entity.*;
import cn.itcast.mapper.OrderMapper;

import cn.itcast.DTO.OrderDTO;
import cn.itcast.pojo.entity.*;
import cn.itcast.VO.OrderDayVO;
import cn.itcast.VO.OrderFlowVO;
import cn.itcast.VO.OrderUserVO;
import cn.itcast.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserClients userClients;
    @Autowired
    private RoomClients roomClients;

    @Autowired
    private CastClients castClients;
    @Autowired
    private NoteClients noteClients;
    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Override
    @GlobalTransactional
    public void addOrder(OrderDTO orderDTO) {
        Order order = new Order();
        User user = new User();
        Room room = new Room();
        // 将OrderDTO对象的属性复制给Order对象
        BeanUtils.copyProperties(orderDTO, order);
        // 将OrderDTO对象的属性复制给User对象
        BeanUtils.copyProperties(orderDTO, user);
        // 设置Room对象的属性
        room.setStatus(1);
        room.setNumber(orderDTO.getRoomNumber());
        // 添加用户
        Result<User> userResult = userClients.addUser(user);
        // 更新房间信息
        roomClients.updateRoomByNumber(room);
        // 设置订单的用户ID和房间ID
        //订单数据添加
        order.setUserId(userResult.getData().getId());
        // 根据房间号查找房间ID
        order.setStatus(1);
        order.setCreateTime(LocalDateTime.now());
        Result<Integer> roomNo = roomClients.findIdByRoomNo(orderDTO.getRoomNumber());
        order.setRoomId((long) roomNo.getData());
        //调用费用模块
        castClients.add(order);
        // 添加订单
        orderMapper.addOrder(order);
    }

    @Override
    public OrderDayVO getOrderDay(Long roomId) {
        // 获取房间id对应的订单金额和实付金额
        Integer orderCashPledge = orderMapper.getOrderCashPledge(roomId);

        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();

        // 计算30天前的时间
        LocalDateTime old = now.plusDays(-30);

        // 获取指定时间段内的订单列表
        List<Order> orderList = orderMapper.getOrderDay(now, old);

        // 初始化天数为0
        Long day = 0L;

        // 遍历订单列表，计算订单的天数总和
        for (Order order : orderList) {
            Duration between = Duration.between(order.getCheckInDate(), order.getCheckOutDate());
            day += between.toDays();
        }

        // 创建OrderDayVO对象，存储订单金额和天数
        OrderDayVO orderDay = new OrderDayVO(orderCashPledge, day);

        // 返回OrderDayVO对象
        return orderDay;
    }

    @Override
    public OrderUserVO getOrderUserId(Integer userId) {
        // 根据用户ID获取订单列表
        List<Order> orderList = orderMapper.getOrderUserId(userId);

        // 初始化现金抵押数量
        Long cashPledge = 0L;

        // 遍历订单列表，统计现金抵押状态为2的订单数量
        for (Order order : orderList) {
            if (order.getCashPledgeStatus() == 2) {
                cashPledge++;
            }
        }

        // 计算现金抵押条件，即订单总数除以现金抵押数量的结果
        Double CashPledgeCondition = (orderList.size() / (double) cashPledge);

        // 创建并返回订单用户视图对象，包含订单总数和现金抵押条件
        OrderUserVO orderUserVO = new OrderUserVO((long) orderList.size(), CashPledgeCondition);
        return orderUserVO;
    }

    @Override
    public Order getOrderByRoomId(Long roomId) {
        // 根据房间ID和状态获取订单信息
        Integer status = 1;
        Order order = orderMapper.getOrderByRoomId(roomId, status);
        return order;
    }

    //修改状态

    @Override
    public void updateOrder(Order order) {
        // 创建一个Room对象，并设置其id属性为订单中的房间id
        Room room = new Room();
        room.setId(order.getRoomId());
        room.setStatus(0);
        // 更新房间信息
        roomClients.updateRoom(room);
        Note note = new Note();
        note.setBeSentPhone("15572907756");
        noteClients.noteAdd(note);
        order.setCashPledgeStatus(2);
        order.setStatus(2);
        order.setCheckOutDate(LocalDateTime.now());
        // 更新订单信息
        orderMapper.updateOrder(order);
/*        Order od = orderMapper.serviceOrder(order);
        Map<String,Object> msg = new HashMap();
        msg.put("userId",od.getUserId());
        msg.put("orderId",od.getId());
        msg.put("beSentPhone","15572907756");
        rabbitTemplate.convertAndSend("ttl.direct","ttl",msg);*/
    }

    @Override
    public OrderFlowVO flow() {
        return null;
    }

    //每日流量
/*
    @Override
    public OrderFlowVO flow() {
         Order flow = orderMapper.flow();
        OrderFlowVO orderFlowVO = new OrderFlowVO();
        if (flow!=null){
            orderFlowVO.setFlow(flow.getFlow());
            orderFlowVO.setFlowDate(flow.getFlowDate());
        }


        return null;
    }
*/


}
